﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShoppingApp2.Common;
using ShoppingApp2.Common.Models;
using System.Data.SqlClient;
using System.Data;
using ShoppingApp2.DAL.Helpers;

namespace ShoppingApp2.DAL
{
    public class UsersDAL : ModelDAL
    {
        #region consts

        private const string SCHEMA = "[ShoppingApp2].[dbo].";
        private const string SQL_COMMAND_CREATE_USER = "uspCreateUser";
        private const string SQL_COMMAND_CREATE_TOKEN_HISTORY = "uspCreateTokenHistoryByUserId";

        private const string SQL_PARAMETER_USER_ID = "@UserId";
        private const string SQL_PARAMETER_EMAIL = "@Email";
        private const string SQL_PARAMETER_PASSWORD = "@Password";
        private const string SQL_PARAMETER_NICKNAME = "@Nickname";
        private const string SQL_PARAMETER_STATUS_ID = "@Status";
        private const string SQL_PARAMETER_ROLE_ID = "@RoleId";
        private const string SQL_PARAMETER_TOKEN = "@Token";

        #endregion

        public UsersDAL(string connectionString) : base(connectionString)
        {
                        
        }

        public UsersDAL(Transaction transaction) : base(transaction)
        {
            //this.transaction = transaction;
        }

        /// <summary>
        /// Create user in database
        /// </summary>
        /// <param name="user">User extended model</param>
        public int? CreateUser(UserModelExt user)
        {
            int? userId;

            try
            {
                SqlCommand command = new SqlCommand(SQL_COMMAND_CREATE_USER, connection, this.transaction.TransactionSql);
                command.CommandType = CommandType.StoredProcedure;
                
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_EMAIL, SqlDbType.VarChar, user.Email));
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_NICKNAME, SqlDbType.VarChar, user.Nickname));
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_PASSWORD, SqlDbType.VarBinary, user.Password.ConvertToByteArray()));
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_ROLE_ID, SqlDbType.Int, user.RoleId));
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_STATUS_ID, SqlDbType.Int, user.Status));

                //connection.Open();
                //using (connection)
                //{

                    string id = ((int)command.ExecuteScalar()).ToString();
                    userId = Convert.ToInt32(id);
                //}
            }
            catch
            {
                throw;
            }

            return userId;
        }

        /// <summary>
        /// Creates token for user in tokens history table
        /// </summary>
        /// <param name="userId">User id</param>
        /// <param name="token">Token (Guid) for this user</param>
        public void CreateTokenInHistory(int userId, string token)
        {
            try
            {
                SqlCommand command = new SqlCommand(SQL_COMMAND_CREATE_TOKEN_HISTORY, connection, this.transaction.TransactionSql);
                command.CommandType = CommandType.StoredProcedure;
                
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_USER_ID, SqlDbType.Int, userId));
                command.Parameters.Add(CreateParameter(SQL_PARAMETER_TOKEN, SqlDbType.Char, token));

                //using (connection)
                //{
                //    connection.Open();

                    // Insert data into database
                    command.ExecuteNonQuery();
                //}
            }
            catch
            {
                throw;
            }
        }

        public string make(string name)
        {
            return name;
        }
    }
}